#!/bin/bash
# 模拟器兼容构建脚本

echo "🔧 准备模拟器兼容构建..."

# 备份原始的 oh-package.json5
cp oh-package.json5 oh-package.json5.backup
cp entry/oh-package.json5 entry/oh-package.json5.backup

# 临时移除 RedPlayer 依赖
echo "📦 临时移除 RedPlayer 依赖..."
sed -i 's/"@rte-xhs\/redplayer": ".*",//g' oh-package.json5
sed -i 's/"@rte-xhs\/redplayer": ".*",//g' entry/oh-package.json5

# 更新构建配置支持模拟器架构
echo "⚙️ 更新构建配置..."
cat > entry/build-profile-emulator.json5 << 'EOF'
{
  "apiType": "stageMode",
  "buildOption": {
    "externalNativeOptions": {
      "abiFilters": ["x86_64", "arm64-v8a"]
    }
  },
  "buildOptionSet": [
    {
      "name": "release",
      "arkOptions": {
        "obfuscation": {
          "ruleOptions": {
            "enable": false,
            "files": [
              "./obfuscation-rules.txt"
            ]
          }
        }
      }
    },
  ],
  "targets": [
    {
      "name": "default"
    },
    {
      "name": "ohosTest",
    }
  ]
}
EOF

# 备份原始构建配置
cp entry/build-profile.json5 entry/build-profile.json5.backup
cp entry/build-profile-emulator.json5 entry/build-profile.json5

echo "🏗️ 开始构建模拟器兼容版本..."
hvigorw clean
hvigorw assembleHap --mode module -p module=entry@default

echo "🔄 恢复原始配置..."
cp oh-package.json5.backup oh-package.json5
cp entry/oh-package.json5.backup entry/oh-package.json5
cp entry/build-profile.json5.backup entry/build-profile.json5

echo "✅ 模拟器兼容构建完成！"
echo "📱 现在可以在模拟器上运行应用，会自动使用原生播放器"